<template>
    <div class="all">
        <div v-if="!isshow" class="bread">首页>>车辆管理>>出车记录</div>
        <div v-if="isshow" class="bread">首页>>财务管理>>订单管理</div>
        <div class="nav">

                <!-- <el-button disabled  type="primary"  plain @click="dialogVisible = true">添加出车记录</el-button>  -->
                <!-- <span>发布时间</span>
                <span>车型：     -->
                     <!-- <el-select v-model="value" placeholder="选择车型" style="width: 240px">
                       <el-option
                         v-for="item in options"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value"
                       />
                     </el-select> -->
                <!-- </span>
                <span>关键字<input></span>
                <span>搜索</span> -->
                <el-form  :inline="true" :model="formInline" class="demo-form-inline">
                  <!-- <el-form-item label="车牌号">
                    <el-input v-model="formInline.user" placeholder="车牌号" clearable />
                  </el-form-item> -->
                  <el-form-item label="出车日期">
                    <el-date-picker
                      v-model="formInline.date"
                      type="daterange"
                      range-separator="到"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      value-format="YYYY-MM-DD"
                      :size="size"
                    />
                  </el-form-item>
                  <el-form-item label="返回日期">
                    <el-date-picker
                      v-model="formInline.date1"
                      type="daterange"
                      range-separator="到"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      value-format="YYYY-MM-DD"
                      :size="size"
                    />
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="chaxun">查询</el-button>
                  </el-form-item>
                </el-form>

        </div>
        <div class="content">
          <el-table 
            v-loading="tableloading"  
            :data="tableData" border size="small" 
            :row-style="{height: '35px'}" 
            :cell-style="{padding: '0'}" 
            style="width: 100% ;height:calc(100% - 150px);" 
            >
            <el-table-column fixed label="序号" type="index" :index="indexAdd" width="60" />
            <!-- <el-table-column fixed prop="xuhao" label="序号" width="60"/> -->
              <el-table-column prop="dingdanhao" label="订单号" width="220" />
              <el-table-column prop="chepaihao" label="车牌号" width="110" />
              <el-table-column prop="chejia" label="车架号" width="110" />
              <el-table-column prop="siji" label="出车司机" width="100" />
              <el-table-column 
              sortable
              prop="chucheriqi" 
              label="出车日期" 
              width="110" 
              column-key="chucheriqi"
              :filters="[
                { text: '1月', value: '-01-' },
                { text: '2月', value: '-02-' },
                { text: '3月', value: '-03-' },
                { text: '4月', value: '-04-' },
                { text: '5月', value: '-05-' },
                { text: '6月', value: '-06-' },
                { text: '7月', value: '-07-' },
                { text: '8月', value: '-08-' },
                { text: '9月', value: '-09-' },
                { text: '10月', value: '-10-' },
                { text: '11月', value: '-11-' },
                { text: '12月', value: '-12-' },
              ]"
              :filter-method="filterHandler"
              />
              <el-table-column 
              sortable  
              prop="fanhuidate" 
              label="返回日期" 
              width="110" 
              column-key="fanhuidate"
              :filters="[
                { text: '1月', value: '-01-' },
                { text: '2月', value: '-02-' },
                { text: '3月', value: '-03-' },
                { text: '4月', value: '-04-' },
                { text: '5月', value: '-05-' },
                { text: '6月', value: '-06-' },
                { text: '7月', value: '-07-' },
                { text: '8月', value: '-08-' },
                { text: '9月', value: '-09-' },
                { text: '10月', value: '-10-' },
                { text: '11月', value: '-11-' },
                { text: '12月', value: '-12-' },
              ]"
              :filter-method="filterHandlerfanhui"
              />
              <el-table-column prop="yuzujin" label="预租金" width="100" />
              <el-table-column prop="yanjin" label="押金" width="100" />
              <el-table-column prop="mudidi" label="目的地" width="100" />
              <el-table-column prop="xiangmu" label="项目名称" width="100" />
              <el-table-column prop="shouche" label="是否收车" width="120" >
                <template #default>
                  <el-button link type="primary" size="small">确认收车</el-button>
                </template>
              </el-table-column>
              <el-table-column prop="licheng" label="行驶里程" width="100" />
              <el-table-column prop="hetong" label="合同上传" width="100" />
             <el-table-column prop="beizhu" label="备注" width="100" />
              <!-- <el-table-column prop="address" label="Address" width="600" /> -->
              <!-- <el-table-column prop="zip" label="Zip" width="120" /> -->
              <el-table-column fixed="right" label="操作" width="120">
                <template #default="scope">
                  <el-button link type="primary" size="small" @click.prevent="bianjiClick(scope.$index)"
                    >编辑</el-button>
                  <el-button link type="primary" size="small" @click.prevent="deleteInfo(scope.$index)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <Page :message="total" :messagesize="size" @get-message="getMessage">000</Page>
        </div>
    </div>

    <!-- 添加记录对话框 -->
     <el-dialog
         v-model="dialogVisible"
         title="添加出车记录"
         width="500"
         :before-close="handleClose"
         style="text-align:center;padding-top: 10px;"
       >
       <!-- 添加记录的表单内容 -->
        <el-form :model="form" label-width="auto" style="max-width: 800px;padding: 0px 40px 40px 40px;">
          <el-form-item label="订单号">
            <el-input v-model="form.dingdanhao" />
          </el-form-item>
          <el-form-item label="车牌号码">
            <el-input v-model="form.chepaihao" />
          </el-form-item>
          <el-form-item inline-message='true' label="出车司机">
            <el-select v-model="form.siji" placeholder="选择司机">
              <el-option label="司机a" value="司机a" />
              <el-option label="司机b" value="司机b" />
            </el-select>
          </el-form-item>

          <el-form-item label="出车日期">
            <el-col :span="11">
              <el-date-picker
                v-model="form.chucheriqi"
                type="date"
                placeholder="选择时间"
                style="width: 100%"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              />
            </el-col>
          </el-form-item> 

          <el-form-item label="预租金">
            <el-input v-model="form.yuzujin" />
          </el-form-item>

          <el-form-item label="押金">
            <el-input v-model="form.yanjin" />
          </el-form-item>

          <el-form-item label="目的地">
            <el-input v-model="form.mudidi" />
          </el-form-item>

          <el-form-item label="项目名称">
            <el-input v-model="form.xiangmu" />
          </el-form-item>

          <el-form-item label="返回日期">
            <el-col :span="11">
              <el-date-picker
                v-model="form.fanhuidate"
                type="date"
                placeholder="选择时间"
                style="width: 100%"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              />
            </el-col>
          </el-form-item> 

          <el-form-item label="是否收车">
            <el-select v-model="form.shouche" placeholder="选择记录类型">
              <el-option label="是" value="是" />
              <el-option label="否" value="否" />
            </el-select>
          </el-form-item>

         <el-form-item label="行驶里程">
            <el-input v-model="form.licheng" />
          </el-form-item>

          <el-form-item label="合同上传">
            <el-input v-model="form.hetong" />
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="form.beizhu" />
          </el-form-item>
        </el-form>

         <template #footer>
           <div class="dialog-footer">
             <el-button @click="dialogVisible = false">取消</el-button>
             <el-button type="primary" @click="addInfo">
               确认
             </el-button>
           </div>
         </template>
     </el-dialog>
       

    <!-- 编辑记录对话框 -->
     <el-dialog
         v-model="bianjiVisible"
         title="修改出车记录"
         width="500"
         :before-close="handleClose"
         style="text-align:center;padding-top: 10px;"
       >
       <!-- 编辑记录的表单内容 -->
       <el-form :model="form1" label-width="auto" style="max-width: 800px;padding: 0px 40px 40px 40px;">
          <el-form-item label="订单号">
            <el-input v-model="form1.dingdanhao" />
          </el-form-item>
          <el-form-item label="车牌号码">
            <el-input v-model="form1.chepaihao" />
          </el-form-item>
          <el-form-item label="车架号">
            <el-input v-model="form1.chejia" disabled />
          </el-form-item>
          <el-form-item inline-message='true' label="出车司机">
            <el-select v-model="form1.siji" placeholder="选择司机">
              <el-option label="司机a" value="司机a" />
              <el-option label="司机b" value="司机b" />
            </el-select>
          </el-form-item>

          <el-form-item label="出车日期">
            <el-col :span="11">
              <el-date-picker
                v-model="form1.chucheriqi"
                type="date"
                placeholder="选择时间"
                style="width: 100%"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              />
            </el-col>
          </el-form-item> 

          <el-form-item label="预租金">
            <el-input v-model="form1.yuzujin" />
          </el-form-item>

          <el-form-item label="押金">
            <el-input v-model="form1.yanjin" />
          </el-form-item>

          <el-form-item label="目的地">
            <el-input v-model="form1.mudidi" />
          </el-form-item>

          <el-form-item label="项目名称">
            <el-input v-model="form1.xiangmu" />
          </el-form-item>

          <el-form-item label="返回日期">
            <el-col :span="11">
              <el-date-picker
                v-model="form1.fanhuidate"
                type="date"
                placeholder="选择时间"
                style="width: 100%"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              />
            </el-col>
          </el-form-item> 

          <el-form-item label="是否收车">
            <el-select v-model="form1.shouche" placeholder="选择记录类型">
              <el-option label="是" value="是" />
              <el-option label="否" value="否" />
            </el-select>
          </el-form-item>

         <el-form-item label="行驶里程">
            <el-input v-model="form1.licheng" />
          </el-form-item>

          <el-form-item label="合同上传">
            <el-input v-model="form1.hetong" />
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="form1.beizhu" />
          </el-form-item>
        </el-form>


         <template #footer>
           <div class="dialog-footer">
             <el-button @click="bianjiVisible = false">取消</el-button>
             <el-button type="primary" @click="xiugai()">
               确认
             </el-button>
           </div>
         </template>
     </el-dialog>       
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import {getChucheList,addChuche,deleteChuche,putChuche} from '../../api/chuche.js'
import {useRouter} from 'vue-router'
import { ElMessage } from 'element-plus'

//按照时间查询
const formInline = reactive({
  // user: '',
  DataType: '',
  date: '',
  date1:''
})

//查询按钮
const chaxun = ()=>{
  console.log('按照时间查询')
  const data = {
    "startDate":"",
    "endDate":"",
    "fanhuiStartDate":"",
    "fanhuiEndDate":""
  }
  console.log(formInline)

  data.startDate = formInline.date[0]
  data.endDate = formInline.date[1]

  data.fanhuiStartDate = formInline.date1[0]
  data.fanhuiEndDate = formInline.date1[1]
  console.log(data)


  getChucheList(0,data).then(res =>{
    console.log('请求回来的')
      tableloading.value = false
      tableData.value = res.data.content
        console.log(tableData.value,'请求回来的')
      const imagesBack = res.data.content

      total.value = Number(res.data.totalElements)
      // total.value = res.data.totalElements || 0
      console.log('total = ', total.value)
      console.log('total type: ', typeof total.value)

      
      console.log(res)
      console.log(tableData.value, '修改之后的')
    }).catch(err =>{
      if(err){
        ElMessage({
             message: '获取数据超时，请刷新页面',
             grouping: true,
             type: 'warning',
             offset:200
           })
           console.log(err)
      }
    })


  // const startDate = formInline.value.date[0]
  // const endDate = formInline.value.date[1]
  // console.log(data)
  // console.log()
  console.log(formInline.value.date)

}


const router = useRouter()
const path = router.currentRoute.value.path
const isshow = ref(false)
if(path === '/caiding'){
  isshow.value = true
}

import Page from '../../zujian/page.vue'

//筛选
const filterHandler = (value, row, column)=>{
  const property = column.property
  console.log(column)
  return row[property].includes(value) 

}

const filterHandlerfanhui =(value, row, column)=>{
  const property = column.property
  return row[property].includes(value) 
}



// 表格加载
const tableloading = ref(false)

//一页的数量
const size = ref()
//翻页功能
const total = ref()
//当前的请求的页数
const count = ref(0)

//翻页组件页数变化
const getMessage = (msg)=>{
  console.log(msg)
  count.value = msg - 1
  getinfo()
}

//添加记录对话框开关
const dialogVisible = ref(false)
//编辑记录对话框开关
const bianjiVisible = ref(false)

const value = ref('')

//车型选择
const options = [
    {
        value: '防撞车',
        label: '防撞车',
    },
    {
        value: '高空作业车',
        label: '高空作业车',
    },
    {
        value: '桥梁检测车',
        label: '桥梁检测车',
    },
    {
        value: '自行走',
        label: '自行走',
    }
]
//表格 标题和内容
// const tableData = [
//     {
//         xuhao: '01',
//         chepaihao: '京a001',
//         dingdanhao: '订单001',
//         siji: '王富贵',
//         chucheriqi: '2022-10-01',
//         yuzujin: '1000',
//         yanjin: '35000',
//         mudidi: '北京',
//         xiangmu: '高速公路',
//         fanhuidate: '2024-02-08',
//         shouche: '确认收车',
//         licheng: '行驶里程',
//         hetong: '',
//         beizhu: ''
//     }
// ]

const tableData = ref()
// 添加记录的表单内容
const form = ref({
        xuhao: '',
        chepaihao: '',
        dingdanhao: '',
        siji: '',
        chucheriqi: '',
        yuzujin: '',
        yanjin: '',
        mudidi: '',
        shixiang: '',
        fanhuidate: '',
        shouche: '',
        licheng: '',
        hetong: '',
        beizhu: ''
})

// 添加记录的表单内容
const form1 = ref()
// const form1 = ref({
//   xuhao: '01',
//   chepaihao: '京a001',
//   dingdanhao: '订单001',
//   siji: '王富贵',
//   chucheriqi: '2022-10-01',
//   yuzujin: '1000',
//   yanjin: '35000',
//   mudidi: '北京',
//   shixiang: '',
//   fanhuidate: '2024-02-08',
//   shouche: '确认收车',
//   licheng: '行驶里程',
//   hetong: '',
//   beizhu: ''
// })

// 编辑车辆记录按钮
const bianjiClick = (index) => {
  console.log(index,"bianji")
    bianjiVisible.value = true
    console.log(tableData.value,"bianji")
    form1.value = tableData.value[index]
    console.log(form1.value,"这里的data是什么")
    console.log('bianji')
}
// 序号增加
const indexAdd = (index)=> {
      const page = count.value + 1 // 当前页码
      const pagesize = 20 // 每页条数
      return index + 1 + (page - 1) * pagesize
}

const totalPages = ref()
//获取出车记录列表
const getinfo = ()=>{
  tableloading.value = true
   getChucheList(count.value,{}).then(res =>{
    tableloading.value = false
      console.log(res)
      tableData.value = res.data.content
      console.log(tableData.value)
      total.value = res.data.totalElements
      totalPages.value = res.data.totalPages
      size.value = res.data.size
   }).catch(err =>{
    ElMessage({
             message: '请求超时',
             grouping: true,
             type: 'error',
             offset:200
           })
   })
}

//添加出车记录信息
const addInfo = ()=>{
  //请求后端接口
  addChuche(form.value).then(res =>{
    console.log(res)
    getinfo()
  })
  //清空添加记录的表单
  form.value = {
        xuhao: '',
        chepaihao: '',
        dingdanhao: '',
        siji: '',
        chucheriqi: '',
        yuzujin: '',
        yanjin: '',
        mudidi: '',
        shixiang: '',
        fanhuidate: '',
        shouche: '',
        licheng: '',
        hetong: '',
        beizhu: ''
}
  dialogVisible.value = false
}

//删除出车记录
const deleteInfo = (idx)=>{
  console.log(idx)
  const id = tableData.value[idx].xuhao
  deleteChuche(id).then(res =>{
    console.log(res)
    getinfo()
  })
}

//修改出车记录
const xiugai = ()=>{
  console.log(form1)
  putChuche(form1.value).then(res =>{
    console.log(res)
    bianjiVisible.value = false
    getinfo()
  })
}

onMounted(()=>{
  getinfo()
})

</script>

<style lang="scss" scoped>
.all {
    background-color: #d6d6d6;
    height: 100%;
    overflow: hidden;
    padding-left: 10px;

    .bread {
        height: 30px;
        background-color: #fff;
        margin-bottom: 10px;
    }

    .nav {
       // height: 40px;
        background-color: #fff;
        margin-bottom: 10px;
        // padding: 5px;

        div {
            span {
                margin: 10px;
                line-height: 40px;
            }
        }
    }

    .content {
        background: #fff;
        height: 100%;
        padding: 5px;
        overflow: hidden;
        .page{
          margin: 10px auto;
        }
    }
}
// 翻页的样式
.example-pagination-block + .example-pagination-block {
  margin-top: 10px;
  text-align: center;
}
.example-pagination-block .example-demonstration {
  margin-bottom: 16px;
}

.demo-form-inline{
  padding:5px;
  height:35px;
}

.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>